<?php

function form_parse_query($query) {
    $token = explode("\n", $query);
    //foreach($token as $line) {
    for ($index = 0; $index < count($token); $index += 1) {
	$line = $token[$index];
	//echo 'line['.$index.']: '.$line.'<br>';
	//$line = explode('=', trim($line));
	$line = trim($line);
	$loc = strpos($line, '=');
	if ($loc != '') {
	    $a = substr($line, 0, $loc);
	    $b = substr($line, $loc + 1, strlen($line) - $loc);
	    $left = explode(':', $a);
	    $id = $left[0];
	    $attribute = $left[1];
	    if ($id == '-') {
		$i += 1;
		$id = 'element_'.$i;
	    }
	    if ($id != '') {
		$old_id = $id;
	    } else {
		$id = $old_id;
	    }
	    
	    //echo $id.':'.$attribute.' = '.$b.'<br>';
	    if (strpos($b, '[[') === FALSE) {
		$b = str_replace("\\'", "'", $b);
		$b = str_replace("\\\"", "&quot;", $b);
	        $cmd[$id.':'.$attribute] = $b;//stripslashes($b);
		
	    } else {		
		if (strpos($b, ']]') === FALSE) {
		    while (strpos($new_line, ']]') === FALSE) {		    
			$new_line = $token[$index];
			$new_value .= $new_line;
			$index += 1;
			$e += 1;
			if ($e > 10000) {
			    break(1);
			}
		    }
		} else {
		    $new_value = $b;
		}
		$start = strpos($new_value, '[[') + 2;
		$end = strpos($new_value, ']]', $start);
		$b = substr($new_value, $start, $end - $start);
		$cmd[$id.':'.$attribute] = $b; //stripslashes($b);
		//echo 'NEWLINE:'.$b.':NEWLINE<br><br>';
		$new_value = '';
		$new_line = '';
	    }
	}
    }
    return $cmd;    
}


function form_generator($prequest='', $output_style='html') {
    global $db_web_root;
    global $db_server_url;
    global $grequest;
    
    if ($prequest == '') {
	$grequest = $_REQUEST;
    } else {
	$grequest = $prequest;
    }

    
    $request = $elements['request'];
    $output_style = $request['output-style'];
    if ($output_style == '') {
	$output_style = 'html';
    }
    
    $id = $request['id'];
    if ($request['output'] != '') {
	$output_style = $request['output'];
    }
    if ($id != '') {
	$query['select'] = $id;
	if ($request['load-dataset'] != '') {
	    $query['dataset'] = $request['load-dataset'];
	} else {
	    $query['dataset'] = $request['dataset'];
	}

	if ($request['data'] == '') {
	    foreach ($elements as $name=>$element) {
		if ($element['db'] != '') {
		    $element['db'] = str_replace('[]', '', $element['db']);
		    $query['get_'.$element['db']] = '';
		}
	    }
	    $dh = db_init_connection($db_server_url, '', '');
	    $results = db_post_query($dh, $query);
	    $records = $results['records'];
	    $record = $records[$id];
	} else {
	    $record = $request['data'];
	}
	foreach ($elements as $name=>$element) {
	    $element['db'] = str_replace('[]', '', $element['db']);
	    if ($element['db'] != '') {
		if ($elements[$name]['value'] == '') {
		    $elements[$name]['value'] = $record[$element['db']];
		    //echo 'set element "'.$name.'" to '.$element['db'].'  "'.$record[$element['db']].'"<br>';
		}
		if ($elements[$name]['value'] == '') {
		    $elements[$name]['value'] = $element['default-value'];
		    //echo 'set element "'.$name.'" to "'.$element['default-value'].'"<br>';
		}		
	    } else {
		if ($element['value'] == '') {
		    if ($element['default-value'] != '') {
			$elements[$name]['value'] = $element['default-value'];
			//echo 'set element "'.$name.'" to "'.$element['default-value'].'"<br>';
		    }
		}
	    }
	}
	
    } else {
	foreach ($elements as $name=>$element) {
	    if ($element['value'] == '') {
		if ($element['default-value'] != '') {
		    $elements[$name]['value'] = $element['default-value'];
		}
	    }
	}
    }
    /*
    echo '<pre>';
    print_r($elements);
    echo '</pre>';*/
    $elements = process_elements($grequest, $elements, true);
    $request = $elements['request'];    
    if (is_array($request)) {
	foreach ($request as $key=>$value) {
	    $value = evaluate($value, $elements);
	    $request[$key] = $value;
	}
    }
    if (is_array($elements['request'])) {
	foreach ($elements['request'] as $key=>$value) {
	    $value = evaluate($value, $elements);
	    $elements['request'][$key] = $value;
	}
    }
    if (is_array($grequest)) {
        foreach ($grequest as $key=>$value) {
	    $value = evaluate($value, $elements);
	    $grequest[$key] = $value;
	}
    }

    
    
    $elements = generate_output($elements, $output_style);

    if ($request['action'] != '') {
	$action_url = $request['action'];
	if (strpos($action_url, '?') === FALSE) {
	    $action_url .= '?'.$request_string;
	} else {
	    $action_url .= '&'.$request_string;
	}
	if ($output_style == 'html') {
	    $page .= '<form action="'.$action_url.'" method="post">';
	}
    } else {
	if ($output_style == 'html') {
	    $page .= '<form action="'.$db_web_root.'form_action.php?'.$request_string.'" method="post">';
	}
    }
    if ($output_style == 'html') {
	$page .= '<div class="form_body">';
	foreach($elements as $name=>$element) {
	    $page .= $element['html'];
	}
	$page .= '</div>';
	$page .= '</form>';
    } elseif ($output_style == 'lua') {
    	foreach($elements as $name=>$element) {
	    $page .= $element['declaration'];
	}	
    
	foreach($elements as $name=>$element) {
	    $page .= $element['lua'];
	}	
    } elseif ($output_style == 'latex') {
	foreach($elements as $name=>$element) {
	    $latex_source .= $element['latex']."\n";	    				
	    //echo $name.' = '.$element['latex'].'<br>';
	}
	
	//echo '<pre>Latex Source:'.$latex_source.'</pre>';
	
	$latex_output = post_url('http://127.0.0.1/latex/latex.php', array('source'=>$latex_source,'output'=>$request['latex-output']));
	//echo $latex_output;
	if ($request['filename'] != '') {
	    global $db_root;
	    savefile($db_root.'files/'.$request['filename'], $latex_output);
	    //echo openfile($db_root.'files/'.$request['filename']);
	    $page .= 'files/'.$request['filename'];
	} else {
	    $page .= $latex_output;
	}
    } elseif ($output_style == 'plain') {
	foreach($elements as $name=>$element) {
	    if ($element['type'] == 'blob') {
		$page .= $element['value'];
	    }
	}	
    }

    
    return $page;
}

function generate_html($elements) {
    foreach ($elements as $name=>$element) {
    if ($condition_value == TRUE) {
	if ($element['db'] == '') {
	    $element['db'] = $name;
	}
	if ($element['type'] == '') {
	    //$element['html'] = $element['value'];
	} elseif ($element['type'] == 'image') {
	    $element['html'] = '<div id="div_'.$name.'" class="form_div" style="'.$element['div-style'].'">
	    <img src="'.$element['src'].'" width="'.$element['width'].'" height="'.$element['height'].'"
	    id="'.$name.'" 
	    ></div>';		
	} elseif ($element['type'] == 'group-start') {
	    if ($element['closed'] != '') {
		$div_style = 'display:none;';
	    } else {
		$div_style = $element['div-style'];
	    }
	    $element['html'] = '<div id="div_'.$name.'"><!-- '.$name.' group start--><span class="group_span" style="'.$element['style'].'"
	    id="span_'.$name.'" '."onclick=\"
    if (document.getElementById('".$name."').style.display == 'none') {
	document.getElementById('".$name."').style.display = '';
    } else {
	document.getElementById('".$name."').style.display = 'none';
    }\"".'>'.$element['caption'].'</span><div id="'.$name.'" class="group_div" style="'.$div_style.'">';
	} elseif ($element['type'] == 'group-end') {
	    $element['html'] = '<!-- '.$name.' group end --></div></div>';
	} elseif ($element['type'] == 'line') {
	    $element['html'] = '<div id="div_'.$name.'" class="form_div" style="'.$element['div-style'].'">
	    <hr style="'.$element['style'].'"></div>';	
	} elseif ($element['type'] == 'button') {
	    $element['html'] = '<div id="div_'.$name.'" class="form_div" style="'.$element['div-style'].'">
	    <input type=submit style="'.$element['style'].'" id="'.$name.'" name="'.$name.'" ';
	    $element['html'] .= 'value="'.$element['caption'].'"></div>';	
	    if ($name == 'submit') {
		$element['html'] .= '<input type=hidden value="'.$element['next_form'].'" name="'.$name.'">';
	    }
	    
	} elseif ($element['type'] == 'blob') {
	    if (is_array($element['value'])) {
		$value_list = $element['value'];
	    } else {
		$element['value'] = str_replace("\\\\", "\\", $element['value']);
		$value_list = array($element['value']);
	    }
	    $count = 0;
	    
	    foreach ($value_list as $e_value) { 
	        $element['html'] .= '<div id="div_'.$name.'" style="'.$element['div-style'].'">
		'.$e_value.'</div>';	
	    }
	} elseif ($element['type'] == 'label') {
	    $element['html'] = '<div id="div_'.$name.'"><div id="div_left_'.$name.'" class="left_div" style="'.$element['div-style'].'">
	<label for="label_'.$name.'" class="form_label"
	id="label_caption_'.$name.'">'.$element['caption'].'</label></div>';	
	    $element['html'] .= '<div id="div_right_'.$name.'" class="right_div" style="'.$element['div-style'].'">
	<span style="'.$element['style'].'"
	id="label_'.$name.'">'.$element['value'].'</span></div></div>';
	} elseif ($element['type'] == 'heading') {
	    if ($element['size'] == 0) {
		$element['size'] = 1;
	    }
	    $element['html'] = '<div id="div_'.$name.'" style="margin-left:10%;">
	<h'.$element['size'].' style="'.$element['style'].'"
	id="heading_'.$name.'">'.$element['value'].'</h'.$element['size'].'></div>';

	} elseif ($element['type'] == 'idbox') {
	    $element['html'] = '<div id="div_'.$name.'" class="form_div" style="'.$element['div-style'].'">
	    <span style="'.$element['style'].'"
	    id="'.$name.'">'.$element['caption'].' '.$id.'</span></div>';	
	} elseif ($element['type'] == 'textbox') {
	    
	    $count = 0;
	    if ($element['active'] != '') {
	        $element['onkeypress'] = 'save_element_to_db(\''.$name.'\', \''.$elements['request']['dataset'].'\', \''.$elements['request']['id'].'\', \''.$element['db'].'\'); ';
	    }
	    if (is_array($element['value'])) {		
		foreach ($element['value'] as $value) { 
		    $value = htmlspecialchars($value);
		    
		    
		    $element['html'] .= '<div id="div_left_'.$name.'" class="left_div">
		    <label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>
		    </div><div id="div_right_'.$name.'" class="right_div"  style="'.$element['div-style'].'">
		    <input type=text style="'.$element['style'].'"
		    size="'.$element['size'].'"	 
		    maxlength="'.$element['maxlength'].'"
		    name="'.$element['db'].'" 
		    id="'.$name.'"';
		    $element['html'] .= ' onkeyup="'.$element['onkeyup'].'" onblur="'.$element['onblur'].'" onkeypress="'.$element['onkeypress'].'" value="'.$value.'">';
		    if ($element['repeat'] == TRUE) {
			$element['html'] .= ' <span onclick="dupe(\'div_'.$name.'\');"><input type=button value="+"></span>';
			/*if ($count > 0) {
			    $element['html'] .= '/ <span onclick="floop(\'div_'.$name.'\');"> lalal</span>';
			}*/
		    }	    
		    $element['html'] .= '</div>';			
		    $count += 1;
		    if ($count == 1) {
			if ($element['repeat'] == TRUE) {
			    $element['html'] = '<div id="div_'.$name.'">'.$element['html'].'</div>';
			}	    		    
		    }
	    	}
	    } else {
		$element['html'] = '<div id="div_left_'.$name.'" class="left_div">
		<label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>
		</div><div id="div_right_'.$name.'" class="right_div"  style="'.$element['div-style'].'">
		<input type=text style="'.$element['style'].'"
		size="'.$element['size'].'"	 
		maxlength="'.$element['maxlength'].'"
		name="'.$element['db'].'" 
		id="'.$name.'"';
		$element['html'] .= ' onkeyup="'.$element['onkeyup'].'" onblur="'.$element['onblur'].'" onkeypress="'.$element['onkeypress'].'"  value="'.htmlspecialchars($element['value']).'">';
		if ($element['repeat'] == TRUE) {
		    $element['html'] .= ' <span onclick="dupe(\'div_'.$name.'\');"><input type=button value="+"></span>';
		}	    
		$element['html'] .= '</div>';	
		if ($element['repeat'] == TRUE) {
		    $element['html'] = '<div id="div_'.$name.'">'.$element['html'].'</div>';
		}	    
	    }
	    
	} elseif ($element['type'] == 'hidden') {
	    $element['html'] = '<input type=hidden style="'.$element['style'].'"
	    name="'.$element['db'].'" 
	    id="'.$name.'" 
	    value="'.htmlspecialchars($element['value']).'">';	
	} elseif ($element['type'] == 'checkbox') {
	    $element['html'] = '<div id="div_left_'.$name.'" class="left_div">
	    <label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>
	    </div><div id="div_right_'.$name.'" class="right_div"  style="'.$element['div-style'].'">
	    <input type=hidden value="" name="'.$element['db'].'">';
	    $element['html'] .= '<input type=checkbox style="'.$element['style'].'"
	    name="'.$element['db'].'" 
	    id="'.$name.'"';
	    if ($element['value'] == TRUE) {
		$element['html'] .= ' CHECKED=CHECKED ';
	    }
	    $element['html'] .= 'value="'.TRUE.'"></div>';	
	} elseif ($element['type'] == 'checkbox-list') {	
	    foreach ($element['list'] as $key=>$value) {	    
		$element['html'] .= '<div id="div_left_'.$key.'-'.$name.'" class="left_div">
		<label id="lbl_'.$name.'" for="'.$key.'-'.$name.'" class="form_label">'.$element['caption'].' '.$value.'</label>
		</div><div id="div_right_'.$key.'-'.$name.'" class="right_div"  style="'.$element['div-style'].'">
		<input type=checkbox style="'.$element['style'].'"
		name="'.$element['db'].'::'.$key.'" 
		id="'.$key.'-'.$name.'"';
		if ($element['value'][$key] == TRUE) {
		    $element['html'] .= ' CHECKED=CHECKED ';
		}
		$element['html'] .= 'value="'.TRUE.'"></div>';	
	    }
	} elseif ( ($element['type'] == 'listbox')|| ($element['type'] == 'combobox') ) {	
	    $value_list = array($element['value']);
	    $count = 0;
	    
	    foreach ($value_list as $e_value) { 
		$element['html'] .= '<div id="div_'.$name.'">';
		$element['html'] .= '<div id="div_left_'.$name.'" class="left_div">
		<label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>
		</div><div id="div_right_'.$name.'" class="right_div"  style="'.$element['div-style'].'">';
		if ($element['type'] == 'combobox') {
		    $element['html'] .= '<input type=text id="'.$name.'" name="'.$element['db'].'" value="'.htmlspecialchars($e_value).'"><br>';
		    $combobox = 'list_';
		}
		$element['html'] .= '<select style="'.$element['style'].'" id="'.$combobox.$name.'"
		size="'.$element['size'].'"';
		if ($element['multiple'] == TRUE) {
		    $element['html'] .= ' multiple name="'.$combobox.$element['db'].'[]"';	
		} else {
		    $element['html'] .= ' name="'.$combobox.$element['db'].'"';	
		}
		$element['html'] .= ' onclick="
		var tbox = document.getElementById(\''.$name.'\');
		var cbox = document.getElementById(\''.$combobox.$name.'\');
		var select_list_selected_index = cbox.selectedIndex;
		var text = cbox.options[select_list_selected_index].text;
		if (text != \'Select:\') {
		    tbox.value = text;
		}
		"';	
		$element['html'] .= ' >';	
		if (is_array($element['list'])) {
		    if ($element['must_select'] == '') {
			if ($e_value == '') {
			    $element['html'] .= '<option selected=selected value="">Select:</option>';
			} else {
			    $element['html'] .= '<option value="">Select:</option>';
			}
		    }	
		    foreach ($element['list'] as $key=>$value) {
			if (is_numeric($key)) {
			    $key = '_'.$key;
			}
			if ($element['no_duplicate_values'] == TRUE) {
			    if (array_search($element['value_list'], $value) === FALSE) {
				$element['value_list'][] = $value;
				$dupe = 0;
			    } else {
				$dupe = 1;
			    }
			} else {
			    $dupe = 0;
			}
		    		
			if ($dupe == 0) {    
			    if (is_array($e_value)) {
				$element_value = $e_value;
				//echo $key.' = '.$element_value[$key].' - '.isset($element_value[$key]).'<br>';
				if (array_search($key, $element_value) === FALSE) {
				    $element['html'] .= '<option value="'.$key.'">'.$value.'</option>';
				} else {
				    $element['html'] .= '<option SELECTED=SELECTED value="'.$key.'">'.$value.'</option>';			    
				}
			    } else {
				if ($e_value == $key) {
				    $element['html'] .= '<option SELECTED=SELECTED value="'.$key.'">'.$value.'</option>';
				} else {
				    $element['html'] .= '<option value="'.$key.'">'.$value.'</option>';
		    		}		
			    }
			}
	    	    }
		}
		$element['html'] .= '</select>';	
		if ($element['add_button'] != '') {
		    $element['html'] .= '<a onmousedown="i = document.getElementById(\'iframe_'.$name.'\'); i.src=\'/intranet/index?form='.$element['form'].'&bare=bare\';" href="#div_add_button_'.$name.'" title="'.$element['title'].'" rel="ibox&width='.$element['width'].'&height='.$element['height'].'&reload_on_close='.$element['reload_on_close'].'&units='.$element['units'].'"><input type=button value="'.$element['add_button_caption'].'"></a>';
		    $element['html'] .= '<div id="div_add_button_'.$name.'" style="display:none;">';
		    $element['html'] .= '<iframe id="iframe_'.$name.'" src="" style="width:100%;height:100%;"></iframe>';
		    $element['html'] .= '</div>';
		    
		}
		if ($element['repeat'] == TRUE) {
		    $element['html'] .= ' <span onclick="dupe(\'div_'.$name.'\');"><input type=button value="+"></span>';
		}	    

		$element['html'] .= '</div>';
		$element['html'] .= '</div>';
	    }
	    
	} elseif ($element['type'] == 'duallistbox') {	
	    $value_list = array($element['value']);
	    $count = 0;
	    
	    foreach ($value_list as $e_value) { 
		$element['html'] .= '<div id="div_'.$name.'">';
		$element['html'] .= '<div id="div_left_'.$name.'" class="left_div" style="">
		<label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>';
		$element['html'] .= '</div><div id="div_right_'.$name.'" class="right_div"  style="width:530px;'.$element['div-style'].'">';


		$element['html'] .= '<div style="float:left;"><select style="width:200px;'.$element['style'].'" id="from_'.$name.'"
		size="'.$element['size'].'"';
		$element['html'] .= ' name="from_'.$element['db'].'"';	
		$element['html'] .= ' ondblclick="
		var fbox = document.getElementById(\'from_'.$name.'\');
		var tbox = document.getElementById(\''.$name.'\');
		var select_list_selected_index = fbox.selectedIndex;
		var text = fbox.options[select_list_selected_index].text;
		var value = fbox.options[select_list_selected_index].value;
		if (text != \'Select:\') {
		    var elOptNew = document.createElement(\'option\');
    		    elOptNew.text = text;
		    elOptNew.value = value;
		    try {
			tbox.add(elOptNew, 0);
		    } catch(ex) {
			tbox.add(elOptNew, elSel.selectedIndex);
		    }	
		    fbox.remove(select_list_selected_index);	    
		    var i = 0;
		    for (i=0;i<tbox.length;i++) {
			if (tbox.options[i].text != \'\') {
			    tbox.options[i].selected = 1;
			} else {
			    tbox.options[i].selected = 0;
			}
	    	    }
		    
		}
		"';	
		$element['html'] .= ' >';	
		if (is_array($element['list'])) {
		    if ($element['must_select'] == '') {
			if ($e_value == '') {
			    $element['html'] .= '<option selected=selected value="">Select:</option>';
			} else {
			    $element['html'] .= '<option value="">Select:</option>';
			}
		    }	
		    foreach ($element['list'] as $key=>$value) {
			if (is_numeric($key)) {
			    $key = '_'.$key;
			}
			if ($element['no_duplicate_values'] == TRUE) {
			    if (array_search($element['value_list'], $value) === FALSE) {
				$element['value_list'][] = $value;
				$dupe = 0;
			    } else {
				$dupe = 1;
			    }
			} else {
			    $dupe = 0;
			}
		    		
			if ($dupe == 0) {    
			    if (is_array($e_value)) {
				$element_value = $e_value;
				//echo $key.' = '.$element_value[$key].' - '.isset($element_value[$key]).'<br>';
				if (array_search($key, $element_value) === FALSE) {
				    $element['html'] .= '<option value="'.$key.'">'.$value.'</option>';
				} else {
				    //$element['html'] .= '<option SELECTED=SELECTED value="'.$key.'">'.$value.'</option>';			    
				}
			    } else {
				if ($e_value == $key) {
				    //$element['html'] .= '<option SELECTED=SELECTED value="'.$key.'">'.$value.'</option>';
				} else {
				    $element['html'] .= '<option value="'.$key.'">'.$value.'</option>';
		    		}		
			    }
			}
	    	    }
		}
		$element['html'] .= '</select></div>';	
		if ($element['repeat'] == TRUE) {
		    $element['html'] .= ' <span onclick="dupe(\'div_'.$name.'\');"><input type=button value="+"></span>';
		}	    

		$element['html'] .= '<div style="float:left;"><select style="width:200px;'.$element['style'].'" multiple id="'.$name.'"
		size="'.$element['size'].'"';
		$element['html'] .= ' name="'.$element['db'].'"';	
		$element['html'] .= ' ondblclick="
		var tbox = document.getElementById(\'from_'.$name.'\');
		var fbox = document.getElementById(\''.$name.'\');
		var select_list_selected_index = fbox.selectedIndex;
		var text = fbox.options[select_list_selected_index].text;
		var value = fbox.options[select_list_selected_index].value;
		if ((text != \'Select:\') && (text != \'\')) {
		    var elOptNew = document.createElement(\'option\');
    		    elOptNew.text = text;
		    elOptNew.value = value;
		    try {
			tbox.add(elOptNew, 0);
		    } catch(ex) {
			tbox.add(elOptNew, elSel.selectedIndex);
		    }	
		    fbox.remove(select_list_selected_index);
		    if (fbox.length == 0) {
			var blank = document.createElement(\'option\');
    			blank.text = \'\';
			blank.value = \'\';
		    
			fbox.add(blank, 0);
			fbox.selectedIndex = 0;
		    }
		}
		" onclick="
		    var i = 0;
		    var tbox = document.getElementById(\''.$name.'\');
		    for (i=0;i<tbox.length;i++) {
			if (tbox.options[i].text != \'\') {
			    tbox.options[i].selected = 1;
			} else {
			    tbox.options[i].selected = 0;
			}
	    	    }		    
		
		"';	
		$element['html'] .= ' >';	
		if (is_array($element['value'])) {
		    foreach ($element['value'] as $key) {
			if (substr($key, 0, 1) == '_') {
			    $l_key = substr($key, 1, strlen($key) - 1);
			}
			if ($element['no_duplicate_values'] == TRUE) {
			    if (array_search($element['value_list'], $value) === FALSE) {
				$element['value_list'][] = $value;
				$dupe = 0;
			    } else {
				$dupe = 1;
			    }
			} else {
			    $dupe = 0;
			}
		    		
			if ($dupe == 0) {    
			    if (is_array($e_value)) {
				$element_value = $e_value;
				//echo $key.' = '.$element_value[$key].' - '.isset($element_value[$key]).'<br>';
				if (array_search($key, $element_value) === FALSE) {
				    //$element['html'] .= '<option value="'.$key.'">'.$value.'</option>';
				} else {
				    $element['html'] .= '<option SELECTED=SELECTED value="'.$key.'">'.$element['list'][$l_key].'</option>';
				}
			    } else {
				if ($e_value == $key) {
				    $element['html'] .= '<option SELECTED=SELECTED value="'.$key.'">'.$element['list'][$key].'</option>';
				} else {
				    //$element['html'] .= '<option value="'.$key.'">'.$value.'</option>';
		    		}		
			    }
			}
	    	    }		
		}
		$element['html'] .= '</select></div>';
		$element['html'] .= '</div>';
		$element['html'] .= '</div>';
	    }
	    

	} elseif ($element['type'] == 'textarea') {
	    $element['html'] = '<div id="div_'.$name.'"><div id="div_left_'.$name.'" class="left_div">
	    <label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>
	    </div><div id="div_right_'.$name.'" class="right_div"  style="'.$element['div-style'].'">
	    <textarea style="'.$element['style'].'"
	    cols="'.$element['cols'].'"	 
	    rows="'.$element['rows'].'"
	    name="'.$element['db'].'" 
	    id="'.$name.'" 
	    >'.htmlspecialchars($element['value']).'</textarea></div></div>';	
	} elseif ($element['type'] == 'password') {
	    $element['html'] = '<div id="div_'.$name.'"><div id="div_left_'.$name.'" class="left_div">
	    <label id="lbl_'.$name.'" for="'.$name.'" class="form_label">'.$element['caption'].'</label>
	    </div><div id="div_right_'.$name.'" class="right_div"  style="'.$element['div-style'].'">
	    <input type=password style="'.$element['style'].'"
	    size="'.$element['size'].'"	 
    	    maxlength="'.$element['maxlength'].'"';
	    if ($element['crypt'] == TRUE) {
		$element['html'] .= 'name="crypt_'.$name.'" ';
	    } else {
		$element['html'] .= 'name="'.$name.'" value="'.$element['value'].'"';
	    }
	    $element['html'] .= 'id="'.$name.'" 
	    ></div></div>';	
	} elseif ($element['type'] == 'modal_form') {
	    $element['html'] = '<a onmousedown="i = document.getElementById(\'iframe_'.$name.'\'); 
	    i.src=\'/intranet/index?form='.$element['form'].'&bare=bare\';" href="#div_'.$name.'" 
	    title="'.$element['title'].'" rel="ibox&width='.$element['width'].'&height='.$element['height'].
	    '&reload_on_close='.$element['reload_on_close'].'&units='.$element['units'].'">'.$element['caption'].'</a>';
	    
	    $element['html'] .= '<div id="div_'.$name.'" style="display:none;">';
	    $element['html'] .= '<iframe id="iframe_'.$name.'" src="" style="width:100%;height:100%;"></iframe>';
	    $element['html'] .= '</div>';
	} elseif ($element['type'] == 'modal_url') {
	
	    $element['html'] = '<div id="div_label_'.$name.'"><div id="div_left_'.$name.'" class="left_div" style="'.$element['div-style'].'">
	<label for="label_'.$name.'" class="form_label"
	id="label_caption_'.$name.'">';


	    $element['html'] .= '<a onmousedown="i = document.getElementById(\'iframe_'.$name.'\'); 
	    i.src=\''.$element['url'].'\';" href="#div_'.$name.'" 
	    title="'.$element['title'].'" rel="ibox&width='.$element['width'].'&height='.$element['height'].
	    '&reload_on_close='.$element['reload_on_close'].'&units='.$element['units'].'">'.$element['value'].'</a>';	

	$element['html'] .= '</label></div>';	

	    $element['html'] .= '<div id="div_right_'.$name.'" class="right_div" style="'.$element['div-style'].'">
	<span style="'.$element['style'].'"
	id="label_'.$name.'">';
	
	    $element['html'] .= $element['caption'];
	    
	    $element['html'] .= '</span></div></div>';
	    
	    $element['html'] .= '<div id="div_'.$name.'" style="display:none;">';
	    $element['html'] .= '<iframe id="iframe_'.$name.'" src="" style="width:100%;height:100%;"></iframe>';
	    $element['html'] .= '</div>';

	}
	if ($element['repeat'] != '') {
	    //$element['html'] .= '<div class="left_div">'.$name.'</div><div class="right_div"><span onclick="dupe(\'div_'.$name.'\');"><input type=button value="+"></span></div>';
	
	}
	$elements[$name] = $element;
    } // condition
    } // foreach
    return $elements;
}

function evaluate($value, $elements) {
    $value = replace_variable($value, $elements);
    return $value;
}

function replace_variable($value, $elements) {
    global $id;
    global $grequest;
    $loc_a = strpos($value, '%_');
    if ($loc_a === FALSE) {
    } else {
	$loc_b = strpos($value, '_%', $loc_a + 1);
	if ($loc_b > $loc_a) {
	    $key = substr($value, $loc_a + 2, $loc_b - $loc_a - 2);
	    $o_key = $key;
	    $loc = strpos($key, ':');
	    if ($loc === FALSE) {
		$new_value = FALSE;
		if ($o_key == 'id') {
		    $new_value = $grequest['request:id'];
		}
		if ($o_key == 'bare') {
		    $new_value = $_REQUEST['bare'];
		}
		if ($o_key == 'time') {
		    $new_value = time();
		}


		if ($o_key == 'user_name') {
		    $new_value = $grequest['request:user_name'];
		}
		if ($o_key == 'user_id') {
		    $new_value = $grequest['request:user_id'];
		}
		if ($new_value === FALSE) {
		} else {
		    $value = str_replace('%_'.$o_key.'_%', $new_value, $value);
		}
	    } else {
		$element_name = substr($key, 0, $loc);	
		$loc_a = strpos($element_name, '[');
		if ($loc_a === FALSE) {
		    $a_index = -1;
		} else {
		    $loc_b = strpos($element_name, ']', $loc_a + 1);
		    if ($loc_b > $loc_a) {
			$a_index = substr($element_name, $loc_a + 1, $loc_b - $loc_a - 1);
		    }
		}
		
	        $key = substr($key, $loc + 1, strlen($key) - $loc);
		
		/*echo 'Element Name: '.$element_name.'<br>';
		echo 'Key: '.$key.'<br>';
		echo 'a_index: '.$a_index.'<br>';*/
		if ($a_index != -1) {
		    $element_name = substr($element_name, 0, strlen($element_name) - (2 + strlen($a_index)));
		    $element = $elements[$element_name];
		    $new_value = $element[$key];
		    //echo '<pre>';
		    //print_r($new_value);
		    //echo '</pre>';
		    if (is_numeric($a_index)) {
			$a_index = '_'.$a_index;
		    }
		    $new_value = $new_value[$a_index];
		    $numeric_new_value = substr($new_value, 1, strlen($new_value) - 1);
		    if (is_numeric($numeric_new_value)) {
			if (!is_numeric($new_value)) {
			    $new_value = $numeric_new_value;
			}
		    }
		} else {
		    $element = $elements[$element_name];
		    $new_value = $element[$key];
		}
		//echo 'Value: '.$value.'<br>';
		if ($element_name == 'request') {
		    $value = str_replace('%_'.$o_key.'_%', $grequest['request:'.$key], $value);
		} elseif ($element_name == 'http') {
		    $value = str_replace('%_'.$o_key.'_%', $_SERVER['HTTP_'.strtoupper($key)], $value);
		} else {
		    $value = str_replace('%_'.$o_key.'_%', $new_value, $value);
		}
		//echo 'Value: '.$value.'<br>';
	    }
	    
	}
    }
    $loc_a = strpos($value, '%_');
    if ($loc_a === FALSE) {
    } else {
	$loc_b = strpos($value, '_%', $loc_a);
	if ($loc_b > $loc_a) {
	    $value = replace_variable($value, $elements);
	}
    }
    return $value;
}

function generate_output($elements, $output_style='html') {
    foreach ($elements as $name=>$element) {
	if (function_exists($element['type'].'_'.$output_style)) {
	    $elements[$name] = call_user_func($element['type'].'_'.$output_style, $element);
	}
    }
    return $elements;
}


php?>